//
//  HeaderTopView.m
//  intelligent
//
//  Created by 甘世清 on 2024/2/8.
//

#import "HeaderTopView.h"
#import "HeaderCollectionViewCell.h"
@implementation HeaderTopView
- (void)SQ_UIViewForSelfView{
    self.backgroundColor = UIColor.whiteColor;
    self.layer.cornerRadius = 20;
    
    [self.collectionView registerClass:[HeaderCollectionViewCell class] forCellWithReuseIdentifier:@"HeaderCollectionViewCell"];
    [self addSubview:self.collectionView];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self).insets(UIEdgeInsetsMake(30, 0, FootHeight, 0));
    }];
    self.jsonArray = [[NSMutableArray alloc]initWithObjects:@"头像0",@"头像1",@"头像2",@"头像3", nil];
    [self.collectionView reloadData];
}

#pragma mark UICollectionViewDelegate,UICollectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.jsonArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath{
    HeaderCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HeaderCollectionViewCell" forIndexPath:indexPath];
    NSString * imagename = [self.jsonArray objectAtIndex:indexPath.item];
    cell.bgImage.image = SQGetImage(imagename);
    if ([imagename isEqualToString:[UserModel share].portraitname]) {
        cell.yesImage.hidden = NO;
    } else {
        cell.yesImage.hidden = YES;
    }
    
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat w = SCREEN_WIDETH/2.f - 40;
    return CGSizeMake(w, 100);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 20;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 30, 0, 30);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [YJProgressHUD SQ_showMsgWithoutView:@""];
    [dic setObject:@(indexPath.item) forKey:@"portrait"];
    [HttpTool post:@"user/config" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        UserModel * model = [UserModel share];
        model.portrait = [NSString stringWithFormat:@"%ld",(long)indexPath.item];
        [UserModel updateLocalUserModel:model];
        if (self.HeaderBlock) {
            self.HeaderBlock();
        }
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];

}
 

- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
//        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.userInteractionEnabled = YES;
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
   
    }
    return _collectionView;
}

-(NSMutableArray*)jsonArray
{
    if (!_jsonArray) {
        _jsonArray = [[NSMutableArray alloc]init];
    }
    return _jsonArray;
}

@end
